<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'sports:sportsTotalGradeRule:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ ruleId: record.ruleId })">
          {{ record.scoreSymbol }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSportsSportsTotalGradeRuleList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { sportsTotalGradeRuleDelete, sportsTotalGradeRuleListData } from '/@/api/sports/sportsTotalGradeRule';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('sports.sportsTotalGradeRule');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('总分&等级规则管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      // {
      //   label: t('分值符号'),
      //   field: 'scoreSymbol',
      //   component: 'Input',
      // },
      // {
      //   label: t('分值1'),
      //   field: 'score1',
      //   component: 'Input',
      // },
      // {
      //   label: t('分值2'),
      //   field: 'score2',
      //   component: 'Input',
      // },
      {
        label: t('总分等级'),
        field: 'totalGrade',
        component: 'Select',
        componentProps: {
          dictType: 's_total_grade',
          allowClear: true,
        },
      },
      {
        label: t('规则分类'),
        field: 'qualityType',
        component: 'Select',
        componentProps: {
          dictType: 's_test_type',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('总分'),
      dataIndex: 'scoreTotal',
      key: 'a.score_symbol',
      //sorter: true,
      width: 230,
      align: 'left',
    },
    // {
    //   title: t('分值符号'),
    //   dataIndex: 'scoreSymbol',
    //   key: 'a.score_symbol',
    //  // sorter: true,
    //   width: 230,
    //   align: 'left',
    //   dictType: 's_scoring_rules',
    // },
    {
      title: t('规则分类'),
      dataIndex: 'qualityType',
      key: 'a.term_gype',
      //sorter: true,
      width: 130,
      align: 'center',
      dictType: 's_test_type',
    },
    {
      title: t('总分等级'),
      dataIndex: 'totalGrade',
      key: 'a.total_grade',
      //sorter: true,
      width: 130,
      align: 'center',
      dictType: 's_total_grade',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      //sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑总分&等级规则'),
        onClick: handleForm.bind(this, { ruleId: record.ruleId }),
        auth: 'sports:sportsTotalGradeRule:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除总分&等级规则'),
        popConfirm: {
          title: t('是否确认删除总分&等级规则'),
          confirm: handleDelete.bind(this, { ruleId: record.ruleId }),
        },
        auth: 'sports:sportsTotalGradeRule:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: sportsTotalGradeRuleListData,
    beforeFetch: (params) => {
      return params;
    },
    afterFetch: (res) => {
      res.forEach((item) => {
        console.log(item.score2);
        if (item.score2) {
          item.scoreTotal = item.score1 + '-' + item.score2;
        } else {
          item.scoreTotal = item.score1;
        }
      });
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await sportsTotalGradeRuleDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
